/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.servico;

import br.com.foxinline.modelo.Secretaria;
import br.com.foxinline.modelo.Setor;
import br.com.foxinline.util.Msg;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author elyjr
 */
@Stateless
public class SecretariaServico extends ServicoGenerico<Secretaria> {

    @EJB
    SetorServico setorServico;

    public SecretariaServico() {
        super(Secretaria.class);
    }

    public void salvar(Secretaria secretaria, List<Setor> setores) {
        secretaria.setAtivo(Boolean.TRUE);
        this.salvar(secretaria);
        for (Setor s : setores) {
            s.setSecretaria(secretaria);
            s.setAtivo(Boolean.TRUE);
            setorServico.salvar(s);
        }
        Msg.messagemInfoRedirect(Msg.SuccessFull, "indexsecretaria.xhtml");
    }

    public void atualizar(Secretaria secretaria, List<Setor> setores) {
        secretaria.setAtivo(Boolean.TRUE);
        this.atualizar(secretaria);
        for (Setor s : setores) {
            if (s.getId() == null) {
                s.setSecretaria(secretaria);
                s.setAtivo(Boolean.TRUE);
                setorServico.salvar(s);
            } else {
                s.setSecretaria(secretaria);
                s.setAtivo(Boolean.TRUE);
                setorServico.atualizar(s);
            }
        }
    }

    public List<Secretaria> pesquisarSecretaria(Secretaria secretaria) {
        String sql = "SELECT s FROM Secretaria s ";

        sql += " where ";

        if ((secretaria.getNome() != null) && !secretaria.getNome().equals("")) {
            sql += " lower(s.nome) like lower(:nome) and ";
        }
        if ((secretaria.getNomeSecretario() != null) && !secretaria.getNomeSecretario().equals("")) {
            sql += " lower(s.nomeSecretario) like lower(:nsec) and ";
        }
        if ((secretaria.getSgl() != null) && !secretaria.getSgl().equals("")) {
            sql += " lower(s.sgl) like lower(:sgl) and ";
        }

        sql += " s.ativo = TRUE AND 1=1 Order by s.id ASC ";

        Query query = getEntityManager().createQuery(sql);

        if ((secretaria.getNome() != null) && !secretaria.getNome().equals("")) {
            query.setParameter("nome", "%" + secretaria.getNome() + "%");
        }
        if ((secretaria.getNomeSecretario() != null) && !secretaria.getNomeSecretario().equals("")) {
            query.setParameter("nsec", "%" + secretaria.getNomeSecretario() + "%");
        }
        if ((secretaria.getSgl() != null) && !secretaria.getSgl().equals("")) {
            query.setParameter("sgl", "%" + secretaria.getSgl() + "%");
        }
        return (List<Secretaria>) query.getResultList();
    }

    /**
     * Pesquisa todos os setores da secretaria informada
     *
     * @param secretaria
     * @return
     */
    public List<Setor> findSetores(Secretaria secretaria) {
        String sql = "SELECT s FROM Secretaria se JOIN se.setores s WHERE se.id = :id AND s.ativo = TRUE";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", secretaria.getId());
        return (List<Setor>) query.getResultList();
    }

    /**
     * Pesquisa a secretaria do setor informado
     *
     * @param setor
     * @return
     */
    public Secretaria find(Setor setor) {
        String sql = "SELECT se FROM Secretaria se JOIN se.setores s WHERE s.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", setor.getId());
        return (Secretaria) query.getSingleResult();
    }

    @Override
    public List<Secretaria> findLimit() {
        String sql = "SELECT s FROM " + Secretaria.class.getSimpleName() + " s WHERE s.ativo = :ativo ORDER BY s.id DESC";
        
        Query query = getEntityManager().createQuery(sql).setMaxResults(10);
        query.setParameter("ativo", true);
        
        return query.getResultList();
    }
}
